package com.eric.husky.service;

import com.eric.husky.common.utils.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.eric.husky.dao.VipDao;
import com.eric.husky.bean.Vip;
import java.util.List;

@Service
public class VipService {
    @Autowired
	private VipDao vipDao;

	public Page<Vip> list(Vip vip, Pageable page) {
		Example<Vip> example = Example.of(vip);
		return vipDao.findAll(example, page);
	}

	public Vip get(Integer vipid) {
		return vipDao.findById(vipid).get();
	}

    public List<Vip> findList() {
		return vipDao.findAll();
	}

	@Transactional
	public Vip save(Vip vip) {
	    if(vip.getVipid()!=null){
	        Vip vipOld = vipDao.findById(vip.getVipid()).orElse(null);
	        if(vipOld!=null){
	            UpdateUtil.copyNullProperties(vip,vipOld);
                return vipDao.save(vipOld);
	        }
	    }
	    return vipDao.save(vip);
	}

	@Transactional
	public void delete(Integer vipid) {
		vipDao.deleteById(vipid);
	}

}